package org.wanxian.stores.db;

import java.util.ArrayList;

import org.wanxian.stores.model.Product;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

public class ProductDao {
	private DatabaseOpenHelper dbHelper;
	private static final String TABLE_NAME = "product";
	
	public ProductDao(Context context) {
		dbHelper = new DatabaseOpenHelper(context);
	}
	
	public boolean isExist(Product product) {
		SQLiteDatabase db = dbHelper.getReadableDatabase();
		Cursor cursor = db.query(TABLE_NAME, null, "id = ?", new String[] {String.valueOf(product.getId())}, null, null, null);
		if(cursor.getCount() > 0) {
			cursor.close();
			db.close();
			return true;
		}
		return false;
	}
	
	public int insert(Product product) {
		Product bef = queryById(product.getId());
		if(bef != null) {
			deleteById(bef.getId());
		}
		SQLiteDatabase db = dbHelper.getWritableDatabase();
		ContentValues values = new ContentValues();
		values.put("storeId", product.getStoreId());
		values.put("name", product.getName());
		values.put("price", product.getPrice());
		values.put("sellNum", product.getSellNum());
		values.put("image", product.getImage());
		int id = (int) db.insert(TABLE_NAME, null, values);
		return id;
	}

	
	public int update(Product product) {
		SQLiteDatabase db = dbHelper.getWritableDatabase();
		ContentValues values = new ContentValues();
		values.put("storesId", product.getStoreId());
		values.put("name", product.getName());
		values.put("price", product.getPrice());
		values.put("sellNum", product.getSellNum());
		values.put("image", product.getImage());
		int index = (int) db.update(TABLE_NAME, values, "id = ?", new String[]{String.valueOf(product.getId())});
		db.close();
		return index;
	}
	
	public void deleteById(int id) {
		SQLiteDatabase db = dbHelper.getWritableDatabase();
		db.delete(TABLE_NAME, "id = ?", new String[] {String.valueOf(id)});
		db.close();
	}
	public Product queryById(int id) {
		Product product = null;
		SQLiteDatabase db = dbHelper.getWritableDatabase();
		Cursor cursor = db.query(TABLE_NAME, null, "id = ?", new String[]{String.valueOf(id)}, null, null, null);
		while(cursor.moveToNext()) {
			String name = cursor.getString(cursor.getColumnIndex("name"));
			int storeId = cursor.getInt(cursor.getColumnIndex("storeId"));
			int price= cursor.getInt(cursor.getColumnIndex("price"));
			int sellNum = cursor.getInt(cursor.getColumnIndex("sellNum"));
			String image = cursor.getString(cursor.getColumnIndex("image"));
			product = new Product(id, storeId, image, name, price, sellNum);
		}
		cursor.close();
		db.close();
		return product;
	}
	
	
	public ArrayList<Product> queryAllByStoreId(int storeId) {
		ArrayList<Product> list = new ArrayList<Product>();
		SQLiteDatabase db = dbHelper.getWritableDatabase();
		Cursor cursor = db.query(TABLE_NAME, null, "storeId = ?", new String[]{String.valueOf(storeId)}, null, null, null);
		while(cursor.moveToNext()) {
			int id = cursor.getInt(cursor.getColumnIndex("id"));
			String name = cursor.getString(cursor.getColumnIndex("name"));
			int price = cursor.getInt(cursor.getColumnIndex("price"));
			int sellNum = cursor.getInt(cursor.getColumnIndex("sellNum"));
			String image = cursor.getString(cursor.getColumnIndex("image"));
			Product product =  new Product(id, storeId, image, name, price, sellNum);
			list.add(product);
		}
		cursor.close();
		db.close();
		return list;
	}
	
	public ArrayList<Product> queryAll() {
		ArrayList<Product> list = new ArrayList<Product>();
		SQLiteDatabase db = dbHelper.getWritableDatabase();
		Cursor cursor = db.query(TABLE_NAME, null, null, null, null, null, null);
		while(cursor.moveToNext()) {
			int id = cursor.getInt(cursor.getColumnIndex("id"));
			int storeId = cursor.getInt(cursor.getColumnIndex("storeId"));
			String name = cursor.getString(cursor.getColumnIndex("name"));
			int price = cursor.getInt(cursor.getColumnIndex("price"));
			int sellNum = cursor.getInt(cursor.getColumnIndex("sellNum"));
			String image = cursor.getString(cursor.getColumnIndex("image"));
			Product product =  new Product(id, storeId, image, name, price, sellNum);
			list.add(product);
		}
		cursor.close();
		db.close();
		return list;
	}
	
	
	public int getCount() {
		return queryAll().size();
	}
}
